#!/usr/bin/perl -w

#===============================================================
# afv_rename
# File ID: f7f4047c-5d36-11df-a766-90e6ba3022ac
# Leser filnavn fra stdin og skifter navn på dem fra gammelt
# afv-format (epoch) til ååååmmddTttmmssZ . Virker forsåvidt på
# alle filer som begynner med et 9- eller 10-sifret nummer.
#
# Character set: UTF-8
# License: GNU General Public License version 2 or later
# ©opyleft 2004 Øyvind A. Holm <sunny@sunbase.org>
#===============================================================

use strict;
use Getopt::Std;

our ($opt_v) =
    (     0);
getopts('v');

$| = 1;

while (<>) {
	chomp();
	if (/^(.*)\/([^\/]+?)$/) {
		my ($Path, $File) =
		   (   $1,    $2);

		if ($File =~ /^(.*?)\b(\d{9,10})\b(.*)$/) {
			my ($First, $Num, $Rest) =
			   (    $1,   $2,    $3);
			my $From = "$Path/$File";
			if (-f $From) {
				my @TA = gmtime($Num);
				my $date_str = sprintf("%04u%02u%02uT%02u%02u%02uZ", $TA[5]+1900, $TA[4]+1, $TA[3], $TA[2], $TA[1], $TA[0]);
				my $Dest = "$Path/$First$date_str$Rest";
				unless (-e $Dest) {
					if (rename($From, $Dest)) {
						$opt_v && print("$From → $Dest\n");
					} else {
						warn("rename(\"$From\", \"$Dest\"): $!\n");
					}
				} else {
					warn("$Dest: File exists.\n");
				}
			} else {
				warn("Ignoring non-regular file $From\n");
			}
		}
	}
}

__END__

=pod

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or (at your 
option) any later version.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program; if not, write to the Free Software Foundation, Inc., 
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut

# vim: set fileencoding=UTF-8 filetype=perl foldmethod=marker foldlevel=0 :
# End of file afv_rename
